﻿using MediatR;
using THE_WIND_RISES.Common.Dtos;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Production_Get.API.Application.Commands.Supplier;
using Production_Get.API.Application.Commands.Technologys;
using Microsoft.AspNetCore.StaticFiles;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using THE_WIND_RISES.Common.Domans.Produce.Supplier;
using Production_Get.API.Infrastructures.Interfaces;
using Production_Get.API.Application.Commands.Procurement;
using Production_Get.API.API.Application.Commands.Procurement;
using Production_Get.API.DTO;
using Production_Get.API.Application.Commands.ReportForm;

namespace Production_Get.API.Controllers.SU
{
    /// <summary>
    /// 生产-工艺，供应商，采购/委外
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class ShowProducManagementController : ControllerBase
    {
        /// <summary>
        /// 中介者
        /// </summary>
        private readonly IMediator mediator;
        /// <summary>
        /// 日志
        /// </summary>
        private readonly ILogger<ShowProducManagementController> logger;
        /// <summary>
        /// 供应商仓储
        /// </summary>
        private readonly IBaseRepositoryGet<Suppliers> suppliersRepository;
        private readonly IBaseRepositoryGet<PurchasingDto> purchasingsRepository;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="mediator"></param>
        /// <param name="logger"></param>
        public ShowProducManagementController(IMediator mediator, ILogger<ShowProducManagementController> logger, IBaseRepositoryGet<Suppliers> suppliersRepository, IBaseRepositoryGet<PurchasingDto> purchasingsRepository)
        {
            this.mediator = mediator;
            this.logger = logger;
            this.suppliersRepository = suppliersRepository;
            this.purchasingsRepository = purchasingsRepository;
        }

        /// <summary>
        /// 工艺路线显示
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Result> ShowProcessRoute(ProcessRouteQueryCommand cmd)
        {
            try
            {
                logger.LogInformation("工艺路线显示");
                return await mediator.Send(cmd);
            }
            catch (Exception ex)
            {
                logger.LogError("工艺路线显示失败" + ex.Message);
                throw;
            }
        }
        /// <summary>
        /// 工序行显示
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Result> ShowOperationLine(OperationLinePageCommand cmd)
        {
            try
            {
                logger.LogInformation("工序行显示");
                return await mediator.Send(cmd);
            }
            catch (Exception ex)
            {
                logger.LogError("工序行显示失败" + ex.Message);
                throw;
            }
        }
        /// <summary>
        /// 物流管理显示
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Result> ShowItemInfo(ItemInfoShowCommand cmd)
        {
            try
            {
                logger.LogInformation("物流管理显示");
                return await mediator.Send(cmd);
            }
            catch (Exception ex)
            {
                logger.LogError("物流管理显示失败" + ex.Message);
                throw;
            }
        }
        /// <summary>
        /// 联系人显示
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Result> ShowContactPersons(ContactPersonsShowCommand cmd)
        {
            try
            {
                logger.LogInformation("联系人显示");
                return await mediator.Send(cmd);
            }
            catch (Exception ex)
            {
                logger.LogError(" 联系人显示失败" + ex.Message);
                throw;
            }
        }
        /// <summary>
        /// 银行显示
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Result> ShowBanks(BanksShowCommand cmd)
        {
            try
            {
                logger.LogInformation("银行显示");
                return await mediator.Send(cmd);
            }
            catch (Exception ex)
            {
                logger.LogError(" 银行显示失败" + ex.Message);
                throw;
            }
        }
        /// <summary>
        /// 供应商分页查询显示
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Result> QuerySuppiler(SupplierQueryCommand cmd)
        {
            try
            {
                logger.LogInformation("供应商分页查询显示");
                return await mediator.Send(cmd);
            }
            catch (Exception ex)
            {
                logger.LogError(" 供应商分页查询显示失败" + ex.Message);
                throw;
            }
        }
        /// <summary>
        /// 供应商反填
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Result> SuppilerSelById(SupplierSelIdCommand cmd)
        {
            try
            {
                logger.LogInformation("供应商反填");
                return await mediator.Send(cmd);
            }
            catch (Exception ex)
            {
                logger.LogError(" 供应商反填失败" + ex.Message);
                throw;
            }
        }

        /// <summary>
        /// 联系人反填
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Result> ContactPersonsSelById(ContactPersonsSelIdCommand cmd)
        {
            try
            {
                logger.LogInformation("联系人反填");
                return await mediator.Send(cmd);
            }
            catch (Exception ex)
            {
                logger.LogError(" 联系人反填失败" + ex.Message);
                throw;
            }
        }
        /// <summary>
        /// 银行反填
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Result> BanksSelById(BanksSelIdCommand cmd)
        {
            try
            {
                logger.LogInformation("银行反填");
                return await mediator.Send(cmd);
            }
            catch (Exception ex)
            {
                logger.LogError(" 银行反填失败" + ex.Message);
                throw;
            }
        }
        /// <summary>
        /// 显示工作中心
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Result> WorkCenterShow(WorkCenterShowCommand cmd)
        {
            try
            {
                logger.LogInformation("显示工作中心");
                return await mediator.Send(cmd);
            }
            catch (Exception ex)
            {
                logger.LogError(" 显示工作中心失败" + ex.Message);
                throw;
            }
        }
        /// <summary>
        /// 显示工序管理
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Result> ProcessShow(ProcessShowCommand cmd)
        {
            try
            {
                logger.LogInformation("显示工序管理");
                return await mediator.Send(cmd);
            }
            catch (Exception ex)
            {
                logger.LogError(" 显示工序管理失败" + ex.Message);
                throw;
            }
        }

        /// <summary>
        /// 供应商导出
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult ExportExcel()
        {
            try
            {
                string sql = "select *from Suppliers where IsDelete=0";

                var list = suppliersRepository.GetAll(sql, default);

                IWorkbook book = new XSSFWorkbook();

                ISheet sheet = book.CreateSheet("Sheet1");

                //第一行标题
                IRow row = sheet.CreateRow(0);
                row.CreateCell(0).SetCellValue("序号");
                row.CreateCell(1).SetCellValue("供应商编码");
                row.CreateCell(2).SetCellValue("供应商名称");
                row.CreateCell(3).SetCellValue("供应商类型");
                row.CreateCell(4).SetCellValue("供应商地址");
                row.CreateCell(5).SetCellValue("供应商电话");
                row.CreateCell(6).SetCellValue("所在城市");
                row.CreateCell(7).SetCellValue("创建时间");
                row.CreateCell(8).SetCellValue("创建人");


                //第二行 写数据
                int i = 1;
                foreach (var item in list)
                {
                    row = sheet.CreateRow(i);
                    row.CreateCell(0).SetCellValue(item.SuppliersId);
                    row.CreateCell(1).SetCellValue(item.SuppliersNo);
                    row.CreateCell(2).SetCellValue(item.SuppliersName);
                    row.CreateCell(3).SetCellValue(item.SuppliersType);
                    row.CreateCell(4).SetCellValue(item.SuppliersPhone);
                    row.CreateCell(5).SetCellValue(item.SuppliersAdder);
                    row.CreateCell(6).SetCellValue(item.CityId);
                    row.CreateCell(7).SetCellValue(item.Handle);
                    row.CreateCell(8).SetCellValue(item.HandleTime.ToString());

                    i++;

                }

                //WorkBook信息到 内存流中
                byte[] bytes = null;

                using (MemoryStream ms = new MemoryStream())
                {
                    book.Write(ms);
                    bytes = ms.ToArray();
                }

                //.xlsx文件
                var mime = new FileExtensionContentTypeProvider().Mappings[".xlsx"];
                return File(bytes, mime, "供应商信息");
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 采购/委外订单分页显示
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Result> PurchasingsQuery(PurchasingsQueryCommand cmd)
        {
            try
            {
                logger.LogInformation("采购/委外订单分页显示");
                return await mediator.Send(cmd);
            }
            catch (Exception ex)
            {
                logger.LogError(" 采购/委外订单分页显示失败" + ex.Message);
                throw;
            }
        }

        /// <summary>
        /// 订单类型显示
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Result> PurchasingTypeShow(PurchasingTypeShowCommand cmd)
        {
            try
            {
                logger.LogInformation("订单类型显示");
                return await mediator.Send(cmd);
            }
            catch (Exception ex)
            {
                logger.LogError(" 订单类型显示失败" + ex.Message);
                throw;
            }
        }
        /// <summary>
        /// 供应商显示
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Result> SupplierShow(SupplierShowCommand cmd)
        {
            try
            {
                logger.LogInformation("供应商显示");
                return await mediator.Send(cmd);
            }
            catch (Exception ex)
            {
                logger.LogError(" 供应商显示失败" + ex.Message);
                throw;
            }
        }
        /// <summary>
        /// 订单行反填
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Result> OrderLinesSelId(OrderLinesSelIdCommand cmd)
        {
            try
            {
                logger.LogInformation("订单行反填");
                return await mediator.Send(cmd);
            }
            catch (Exception ex)
            {
                logger.LogError(" 订单行反填失败" + ex.Message);
                throw;
            }
        }
        /// <summary>
        /// 付款计划信息反填
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Result> PaymentPlansSelId(PaymentPlansSelIdCommand cmd)
        {
            try
            {
                logger.LogInformation("付款计划信息反填");
                return await mediator.Send(cmd);
            }
            catch (Exception ex)
            {
                logger.LogError(" 付款计划信息反填失败" + ex.Message);
                throw;
            }
        }
        /// <summary>
        /// 订单行显示
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Result> OrderLinesShow(OrderLinesShowCommand cmd)
        {
            try
            {
                logger.LogInformation("订单行显示");
                return await mediator.Send(cmd);
            }
            catch (Exception ex)
            {
                logger.LogError(" 订单行显示失败" + ex.Message);
                throw;
            }
        }
        /// <summary>
        /// 付款计划信息显示
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Result> PaymentPlansShow(PaymentPlansShowCommand cmd)
        {
            try
            {
                logger.LogInformation("付款计划信息显示");
                return await mediator.Send(cmd);
            }
            catch (Exception ex)
            {
                logger.LogError(" 付款计划信息显示失败" + ex.Message);
                throw;
            }
        }

        /// <summary>
        /// 显示
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Result> WorkOrderBoardShow(WorkOrderBoardShowCommand cmd)
        {
            try
            {
                logger.LogInformation("显示");
                return await mediator.Send(cmd);
            }
            catch (Exception ex)
            {
                logger.LogError(" 显示失败" + ex.Message);
                throw;
            }
        }

        /// <summary>
        /// 根据条件查询  审批流程表里 对应的编号
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Result> WorkflowInstanceQuery(WorkflowInstanceCommand cmd)
        {
            try
            {
                logger.LogInformation("显示");
                return await mediator.Send(cmd);
            }
            catch (Exception ex)
            {
                logger.LogError(" 显示失败" + ex.Message);
                throw;
            }
        }

        /// <summary>
        /// 采购/委外订单导出
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult PurchasingsExportExcel()
        {
            try
            {
                string sql = "select a.PurchasinId,a.BillMaking,a.HandleTime,a.PurchasingNo,a.PurchasingName,a.SignUpTime,a.PurchasingStatus,a.PurchasingTypeId,b.PurchasingTypeName,a.ProjectId,c.ProjectName,a.SuppliersId,d.SuppliersName,a.DepartmentId,e.DepartmentName from Purchasings a inner join PurchasingType b on a.PurchasingTypeId=b.PurchasingTypeId join Project c on a.ProjectId=c.ProjectId join Suppliers d on a.SuppliersId=d.SuppliersId join Department e on a.DepartmentId=e.DepartmentId where a.IsDelete=0";
                PurchasingDto come = new PurchasingDto();
                var list = purchasingsRepository.GetAll(sql, come);

                IWorkbook book = new XSSFWorkbook();

                ISheet sheet = book.CreateSheet("Sheet1");

                //第一行标题
                IRow row = sheet.CreateRow(0);
                row.CreateCell(0).SetCellValue("序号");
                row.CreateCell(1).SetCellValue("订单编码");
                row.CreateCell(2).SetCellValue("订单名称");
                row.CreateCell(3).SetCellValue("订单类型");
                row.CreateCell(4).SetCellValue("所属项目");
                row.CreateCell(5).SetCellValue("供应商");
                row.CreateCell(6).SetCellValue("签订日期");
                row.CreateCell(7).SetCellValue("创建时间");
                row.CreateCell(8).SetCellValue("制单人");
                row.CreateCell(9).SetCellValue("制单部门");


                //第二行 写数据
                int i = 1;
                foreach (var item in list)
                {
                    row = sheet.CreateRow(i);
                    row.CreateCell(0).SetCellValue(item.PurchasinId);
                    row.CreateCell(1).SetCellValue(item.PurchasingNo);
                    row.CreateCell(2).SetCellValue(item.PurchasingName);
                    row.CreateCell(3).SetCellValue(item.PurchasingTypeName);
                    row.CreateCell(4).SetCellValue(item.ProjectName);
                    row.CreateCell(5).SetCellValue(item.SuppliersName);
                    row.CreateCell(6).SetCellValue(item.SignUpTime);
                    row.CreateCell(7).SetCellValue(item.HandleTime.ToString());
                    row.CreateCell(8).SetCellValue(item.BillMaking);
                    row.CreateCell(9).SetCellValue(item.DepartmentName);

                    i++;

                }

                //WorkBook信息到 内存流中
                byte[] bytes = null;

                using (MemoryStream ms = new MemoryStream())
                {
                    book.Write(ms);
                    bytes = ms.ToArray();
                }

                //.xlsx文件
                var mime = new FileExtensionContentTypeProvider().Mappings[".xlsx"];
                return File(bytes, mime, "采购/委外订单信息.xlsx");
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
